import NProgress from 'nprogress'
import router, {asyncAddRouter} from "@/common/router/index.js";
import 'nprogress/nprogress.css'
import {getAccessToken, logOutRemoveCache} from "@/common/utils/cache.js";
import useRouteStore from "@/common/store/routerStore.js";
import $modal from "@/common/utils/modal.js";
import {apiUserHasMenu} from "@/api/system.js";

NProgress.configure({showSpinner: false});

const dontLogin = ['/login', '/register', '/public/', '/errNetwork', '/jianli'];

router.beforeEach((to, from, next) => {
    NProgress.start()
    let accessToken = getAccessToken();
    let toPath = to.path;
    let {title} = to.meta;

    // 更新窗口Title
    document.title = title;
    // console.log(" accessToken ", accessToken)

    //已登录
    if (accessToken) {
        //已登录：访问不需要登录页面，踢到首页
        // console.log(toPath)
        // console.log(useRouteStore().menuAll)

        if (toPath === '/errNetwork') {
            next()
            return;
        }

        //已登录：访问不需要登录页面，踢到首页
        if (dontLogin.indexOf(toPath) !== -1) {
            next({path: '/'})
            return;
        }

        //已登录：但是页面刷新了，需要请求服务端菜单
        if (!useRouteStore().menuAll || useRouteStore().menuAll.length === 0) {
            apiUserHasMenu().then(res => {
                //没有菜单，直接退出
                if (res.data.length === 0) {
                    $modal.warning("当前账号不具备管理端功能");
                    logOutRemoveCache()
                    next({path: '/index'})
                } else {
                    useRouteStore().menuAll = res.data

                    // 不具备管理后台页面，直接退出
                    if (!useRouteStore().menuAll || useRouteStore().menuAll.length === 0) {
                        logOutRemoveCache()
                    }

                    // console.log("useRouteStore().menuAll========>", useRouteStore().menuAll)
                    useRouteStore().menuLevel0 = res.data.filter(e => e.level == "0")
                    useRouteStore().menuLevel1 = res.data.filter(e => e.level == "1")
                    useRouteStore().menuLevel2 = res.data.filter(e => e.level == "2")
                    useRouteStore().menuFunction = res.data.filter(e => e.level == "3")
                    // console.log("menu All ========>", useRouteStore().menuAll)
                    // console.log("menuLevel  0 ========>", useRouteStore().menuLevel0)
                    // console.log("menuLevel  1 ========>", useRouteStore().menuLevel1)
                    // console.log("menuLevel  2========> ", useRouteStore().menuLevel2)
                    // console.log("menu  Function ========>", useRouteStore().menuFunction)
                    asyncAddRouter(useRouteStore().menuAll).then(() => {
                        // hack方法 确保addRoutes已完成
                        next({...to, replace: true})
                    })
                }
            });
            return;
        }

        //已登录：状态正常，直接通过
        next()
        return;
    }

    //未登录情况：↓↓↓↓
    let isPublic = dontLogin.indexOf(toPath) !== -1;
    //未登录:访问非登录页面，直接放行
    if (isPublic) next();
    //未登录:访问需要登录的页面，踢到登录页
    if (!isPublic) next(`/login?redirect=${to.fullPath}`);

})

router.afterEach(() => {
    NProgress.done()
})
